﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace WebHousingInfo
{
    public partial class XemThemTinTuc : System.Web.UI.Page
    {
        private static string mode = null;
        private static int savepage = 0, currentpage, numpage;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                mode = Request.QueryString["idcate"];
                string linkpaging = "XemThemTinTuc.aspx?idcate=" + mode;
                numpage = 10;
                string current = null;
                current = Request.QueryString["page"];
                if (current != null)
                    currentpage = int.Parse(current);
                else currentpage = 1;
                int totalrow = gettotalpage();
                int totalpage = totalrow / numpage;
                if (totalrow % numpage != 0)
                    totalpage += 1;
                lblpaging.Text = "";
                if (currentpage > 1)
                {
                    lblpaging.Text += "<ul><li><a href=\"" + linkpaging + "&page=1\">◄◄</a></li>";
                    lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + (currentpage - 1).ToString() + "\">◄</a></li>";
                }
                else
                {
                    lblpaging.Text += "<ul><li><a>◄◄</a></li>";
                    lblpaging.Text += "<li><a>◄</a></li>";
                }
                int custom = 5;
                if (currentpage > custom)
                {
                    if (currentpage > savepage)
                    {
                        if (currentpage + custom <= totalpage)
                        {
                            for (int i = currentpage; i <= currentpage + custom; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                        else
                        {
                            for (int i = totalpage - custom; i <= totalpage; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                    }
                    else
                    {
                        if (currentpage - custom >= 1)
                        {
                            for (int i = currentpage - custom; i <= currentpage; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                        else
                        {
                            for (int i = 1; i <= currentpage; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                    }
                }
                else
                {
                    if (custom > totalpage)
                        custom = totalpage;
                    for (int i = 1; i <= custom; i++)
                    {
                        if (i == currentpage)
                        {
                            lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                        }
                        else  // Trang khác
                        {
                            lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                        }

                    }// end for
                }

                if (currentpage < totalpage)
                {
                    lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + (currentpage + 1).ToString() + "\">►</a></li>";
                    lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + totalpage.ToString() + "\">►►</a></li></ul>";
                }
                else
                {
                    lblpaging.Text += "<li><a>►</a></li>";
                    lblpaging.Text += "<li><a>►►</a></li></ul>";
                }
                savepage = currentpage;
            }
        }
        public bool checkurlimage(string imagestring)
        {
            var pathFileImage = Server.MapPath("TotalUpload/ImagesTotal/" + imagestring);
            if (File.Exists(pathFileImage))
            {
                return true;
            }
            else return false;
        }

        public NewsBOList getdatasource()
        {
            if (mode != null)
            {
                NewsBOList newsbolist = NewsBOList.GetID_NewsBOList(Convert.ToInt32(mode), currentpage, numpage);
                return newsbolist;
            }            
            else return null;
        }

        public int gettotalpage()
        {
            int total = 0;
            if (mode != null)
            {
                total = NewsBOList.GetID_NewsBOList(Convert.ToInt32(mode), -1, -1).Count;
            }         
            return total;
        }
    }
}
